package com.keshettv.kem;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DuplicateResponsesFinder {
	private Map<Integer, List<File>> _duplicatedFiles;

	private class CacheDirectoryProcessor implements Runnable {
		// One day ago.
		final long _xDaysAgo = System.currentTimeMillis() - (1 * 24 * 60 * 60 * 1000L);
		private File _dir;

		CacheDirectoryProcessor(File dir) {
			_dir = dir;
		}
		private void processCacheDirectory(File dir) {
			File[] subDirs = dir.listFiles(new FileFilter() {
				public boolean accept(File pathname) {
					boolean res;
					res = pathname.isDirectory();
					return res;
				}
			});

			if (subDirs != null && subDirs.length > 0) {
				for (File subDir : subDirs) {
					File[] files = subDir.listFiles(new FileFilter() {
						public boolean accept(File pathname) {
							boolean res;
							res = pathname.lastModified() > _xDaysAgo && pathname.isFile();
							return res;
						}
					});

					if (files != null && files.length > 0) {
						for (File file : files) {
							try {
								ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file), 65536));
								CachedContent cachedContent = (CachedContent) ois.readObject();
								ois.close();
								int len = cachedContent.getBody().length;
								List<File> duplicatedFiles = _duplicatedFiles.get(len);
								if (duplicatedFiles == null) {
									duplicatedFiles = new ArrayList<File>(11);
									_duplicatedFiles.put(len, duplicatedFiles);
								}
								duplicatedFiles.add(file);
							} catch (Exception e) {
								System.err.println("Cannot read file " + file.getAbsolutePath() + " due to " + e.getMessage());
							}
						}
					}
				}
			}
		}

		public void run() {
			File[] subDirs = _dir.listFiles(new FileFilter() {
				public boolean accept(File pathname) {
					boolean res;
					res = pathname.isDirectory();
					return res;
				}
			});
			if (subDirs != null && subDirs.length > 0) {
				for (File subDir : subDirs) {
					processCacheDirectory(subDir);
				}
			}
		}
	}

	DuplicateResponsesFinder() {
		_duplicatedFiles = new HashMap<Integer, List<File>>();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DuplicateResponsesFinder drf = new DuplicateResponsesFinder();
		// \\cache03.keshet.prd\Render\0X\0Y\0Z
		Thread[] cdps = new Thread[10];//TODO 10
		for(int i=0; i< cdps.length; i++ ) {
			cdps[i] = new Thread(drf.new CacheDirectoryProcessor(new File("\\\\cache03.keshet.prd\\Render\\0"+i)));
			cdps[i].start();
		}
		// Wait all the threads to complete.
		for(int i=0; i< cdps.length; i++ ) {
			try {
				cdps[i].join();
			} catch (InterruptedException e) {
				// Do nothing.
			}
		}
		for (Map.Entry<Integer, List<File>> me : drf._duplicatedFiles.entrySet()) {
			List<File> files = me.getValue();
			if(files.size() > 1) {
				System.out.println("---- " + me.getKey() + "\t bytes:");
				for(File file: files) {
					System.out.println(file.getAbsolutePath());
				}
			}
		}
	}

}
